<?php

namespace App\Jobs;

use App\Services\Admin\User;
use App\Services\Admin\UserSubscribeService;
use App\Services\Api\EasyWeChatService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\UserSubscribe;
use App\Models\ClassBaseInfo;
use App\Services\Api\SubscribeServices;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class CloseOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $order;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(UserSubscribe $order,$delay,$id)
    {
        $this->order = $order->where('id',$id)->first();
        // 设置延迟的时间，delay() 方法的参数代表多少秒之后执行
        $this->delay($delay);
        Log::info('订单隊列加入成功-'.'id-'.$id.'秒-'.$delay);
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
//        Log::info('测试队列触发');
//
//        $new_time = date("Y-m-d H:i:s");
//
//        $array = [
//            $this->order->subscribe_status,
//            $this->order->class_time,
//            $new_time
//        ];
//        $json_string = json_encode($array);
//
//        Log::info($json_string);

        if($this->order->subscribe_status == 1){
            DB::beginTransaction();
            try{

                $this->order->subscribe_status = 3;

                $this->order->save();

                $msg_time_day = date("Y-m-d",strtotime("{$this->order->class_time}"));

                $start_class_time_array = date("H:i",strtotime("{$this->order->class_time}"));

                $end_class_time_array = date("H:i",strtotime("{$this->order->end_time}"));

                $msg_class_time_string = "{$start_class_time_array}-{$end_class_time_array}";

                $msg_result = [
                    '已到時間上課，系統自動取消排隊，退還對應積分以及現金卷',
                    "+{$this->order->use_integral}分",
                    $msg_time_day,
                    $msg_class_time_string,
                    $this->order->class_name
                ];

                //扣减积分
                $remark = implode("/",$msg_result);

                if(!empty($this->order->user_vip_use_record_id)){
                    UserSubscribeService::appointment_queue_vip_remark($this->order, '已到時間上課，系統自動取消排隊，退還對應的vip');
                }else {
                    UserSubscribeService::cancel_return_consumption($this->order, $remark);
                }

                //发送消息退出排隊
                $user_info = User::info($this->order->user_id);

                $data = [
                    'character_string1'=>['value'=>$user_info->user_number],            //会员编号
                    'thing2'=>['value'=>$this->order->class_name],                      //課程名稱
                    'thing3'=>['value'=>$this->order->store_name],                      //上课地点
                    'time4'=>['value'=>$this->order->class_time],                        //日期
                    'thing5'=>['value'=>'退出排隊'],                                      //預約狀態
                ];

                EasyWeChatService::sendUserMessage($this->order->user_id, $data);

                DB::commit();

            }catch (\Exception $e) {
                //接收异常处理并回滚
                DB::rollBack();
                Log::info('取消排隊失敗-'.$e->getMessage());
            }
        }
    }
}
